import request from "@/utils/request"

const scope = "scope_tbyf"

/**
 * 页面登录
 */
export const login = (username, password) => {
  const grant_type = "password"
  return request({
    url: "/auth/oauth/token",
    headers: {
      isToken: false,
      TENANT_ID: "1",
      Authorization: "Basic cGlnOnBpZw=="
    },
    method: "post",
    params: { username, password, grant_type, scope }
  })
}

/**
 * 保存登录用户到redis
 */
export function saveCurrentUser(data) {
  return request({
    url: "/upms/public/saveCurrentUser",
    method: "post",
    data
  })
}

/**
 * 获取坐诊科室
 */
export function getMedicalStaffReg(username) {
  return request({
    url: `/basic/hisMedicalStaffDept/getListByUserName/${username}`,
    method: "get"
  })
}

export const loginByUsername = (username, password) => {
  const grant_type = "password"
  return request({
    url: "/auth/oauth/token",
    headers: {
      isToken: false,
      Authorization: "Basic c3lzdGVtX21ncjpzeXN0ZW1fbWdy"
    },
    method: "post",
    params: { username, password, grant_type, scope }
  })
}

export function updateUserDetails(obj) {
  return request({
    url: "/upms/user/updateUserInfoDeptId",
    method: "post",
    data: obj
  })
}

export const refreshToken = refresh_token => {
  const grant_type = "refresh_token"
  return request({
    url: "/auth/oauth/token",
    headers: {
      isToken: false,
      Authorization: "Basic c3lzdGVtX21ncjpzeXN0ZW1fbWdy"
    },
    method: "post",
    params: { refresh_token, grant_type, scope }
  })
}

export const getUserInfo = () => {
  return request({
    url: "/upms/user/info",
    method: "get"
  })
}

export const logout = () => {
  return request({
    url: "/auth/token/logout",
    method: "delete"
  })
}

export const getEhrLoginSsoParams = () => {
  return request({
    url: "/upms/user/getEhrLoginSsoParams",
    method: "get"
  })
}

export function getEhrSfjdParams(data) {
  return request({
    url: `/upms/user/getEhrSfjdParams`,
    method: "post",
    params: data
  })
}

export const HisToEhr_isExistRecord = query => {
  return request({
    url: "/health/HisToEhr_isExistRecord.action",
    method: "get",
    params: query
  })
}

export function getEhrGrjkdaParams(data) {
  return request({
    url: `/upms/user/getEhrGrjkdaParams`,
    method: "post",
    params: data
  })
}

export function getEhrGrxjdaParams(data) {
  return request({
    url: `/upms/user/getEhrGrxjdaParams`,
    method: "post",
    params: data
  })
}

export function getEhrGrsfParams(data) {
  return request({
    url: `/upms/user/getEhrGrsfParams`,
    method: "post",
    params: data
  })
}

export function getHealthPlatformParams(obj) {
  return request({
    url: `/upms/user/getHealthPlatformParams`,
    method: "post",
    data: obj
  })
}

export function getEmrDzblParams(data) {
  return request({
    url: `/upms/user/getEmrDzblParams`,
    method: "post",
    params: data
  })
}

export const loginByMobile = (mobile, code) => {
  const grant_type = "mobile"
  return request({
    url: "/auth/mobile/token/sms",
    headers: {
      Authorization: "Basic c3lzdGVtX21ncjpzeXN0ZW1fbWdy"
    },
    method: "post",
    params: { mobile: "SMS@" + mobile, code: code, grant_type }
  })
}

export const loginBySocial = (state, code) => {
  const grant_type = "mobile"
  return request({
    url: "/auth/mobile/token/social",
    headers: {
      Authorization: "Basic c3lzdGVtX21ncjpzeXN0ZW1fbWdy"
    },
    method: "post",
    params: { mobile: state + "@" + code, grant_type }
  })
}
